package cn.arifun.controller;

import cn.arifun.domain.R;
import cn.arifun.entity.SysUser;
import cn.arifun.service.SysUserService;
import jakarta.annotation.Resource;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.List;

/**
 * @Author: 漆原
 * @Description: 系统用户控制器
 * @DateTime: 2024/10/1 15:37
 **/
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private SysUserService userService;

    /**
     * 查询全部用户
     *
     * @return 系统用户列表
     */
    @GetMapping("/getAll")
    public List<SysUser> getAllUsers() {
        return userService.list();
    }

    /**
     * 新增系统用户
     *
     * @param sysUser 系统用户
     * @return 响应
     */
    @PostMapping("/addUser")
    public String addUser(@RequestBody SysUser sysUser) {
        userService.addUser(sysUser);
        return "新增成功";
    }

    /**
     * 获取已登录用户信息
     *
     * @return 用户信息
     */
    @GetMapping("/getLoginUserInfo")
    public R getLoginUserInfo() {
        SecurityContext context = SecurityContextHolder.getContext();
        Authentication authentication = context.getAuthentication();
        Object principal = authentication.getPrincipal();   //用户名
        Object credentials = authentication.getCredentials();   //密码信息
        Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();   //权限集合

        return new R(200, "查询成功", authentication);
    }
}
